<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card>
        <el-tabs>
          <el-tab-pane label="登录账户设置">
            <!-- 放置表单 -->
            <el-form v-model="userInfo" label-width="120px" style="margin-left: 120px; margin-top:30px" :rules="rules">
              <el-form-item label="姓名:">
                <el-input v-model="userInfo.username" style="width:300px" />
              </el-form-item>
              <el-form-item label="密码:">
                <el-input v-model="userInfo.showPassword" style="width:300px" type="password" />
              </el-form-item>
              <el-form-item>
                <el-button type="primary" @click="updateUserInfo()">更新</el-button>
              </el-form-item>
            </el-form>
          </el-tab-pane>
          <el-tab-pane label="个人详情">
            <userInfo />
            <!-- 组件的渲染也可以使用动态组件   <component :is="userInfo"  /> -->
          </el-tab-pane>
          <el-tab-pane label="岗位信息">
            <job-info />
          </el-tab-pane>
        </el-tabs>
      </el-card>
    </div>
  </div>
</template>

<script>
import { getUserDetailById } from '@/api/user'
import { saveUserDetailById } from '@/api/employees'
import userInfo from './components/user-info'
import jobInfo from './components/job-info'
export default {
  components: {
    userInfo,
    jobInfo
  },
  data() {
    return {
      userId: this.$route.params.id,
      userInfo: {
        username: '',
        showPassword: ''
      },
      rules: {
        username: [{ required: true, message: '姓名不能为空', trigger: 'blur' }],
        password2: [{ required: true, message: '密码不能为空', trigger: 'blur' },
          { min: 6, max: 9, message: '密码长度6-9位', trigger: 'blur' }]
      }
    }
  },
  created() {
    this.getUserDetailById()
  },
  methods: {
    async getUserDetailById() {
      this.userInfo = await getUserDetailById(this.userId)
      // console.log(this.userInfo)
    },
    updateUserInfo() {
      // userInfo中的密码还是原密码，这里做一个手动赋值为表单绑定的新密码进行传递，因为接口中字段名为password
      this.$confirm('确定更新用户信息?').then(async() => {
        try {
          await saveUserDetailById({ ...this.userInfo, password: this.userInfo.showPassword })
          this.getUserDetailById()
          this.$message.success('更新成功！')
        } catch (error) {
          console.log(error)
        }
      }).catch(() => {
        this.$message.warning('取消更新！')
      })
    }
  }
}
</script>

<style>

</style>
